﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Universe.TrafficProfiler.UserSettings;

namespace Universe.TrafficProfiler
{
    partial class ProcessListForm
    {

        void SaveSettings()
        {
            ProcessListSettings prev = ProcessListSettings.IO.Value;

            if (this.WindowState != FormWindowState.Minimized)
            {
                ProcessListSettings v =
                    new ProcessListSettings()
                        {
                            IsMaximazied = this.WindowState == FormWindowState.Maximized,
                            Location = this.Location,
                            Size = Size,
                            Processes = GridLayout.Create(dataGridView1)
                        };

                if (this.WindowState == FormWindowState.Maximized)
                {
                    v.Location = prev.Location;
                    v.Size = prev.Size;
                }

                ProcessListSettings.IO.Value = v;
            }
        }

        bool ApplySettings()
        {
            try
            {
                ProcessListSettings v = ProcessListSettings.IO.Value;
                if (v != null)
                {
                    this.Location = v.Location;
                    this.Size = v.Size;
                    if (v.IsMaximazied)
                        this.WindowState = FormWindowState.Maximized;

                    if (v.Processes != null)
                        v.Processes.Apply(dataGridView1);

                    return true;
                }

            }
            catch(Exception ex)
            {
            }

            return false;

        }

        private void Grid_ColumnWidthChanged(object sender, EventArgs e)
        {
            if (!IsApplingGrid)
            {
                try
                {
                    SaveSettings();
                }
                finally
                {
                }
            }
        }

        private void ApplyGridSettings()
        {
            try
            {
                ProcessListSettings settings = ProcessListSettings.IO.Value;
                if (settings != null)
                {
                    try
                    {
                        IsApplingGrid = true;
                        settings.Processes.Apply(dataGridView1);
                    }
                    finally
                    {
                        IsApplingGrid = false;
                    }
                }
            }
            catch (Exception)
            {
            }
        }

    }
}
